<template>
  <div class="app-container">
    <el-row>
      <el-select v-model="query.time" placeholder="请选择" style="width: 80px">
        <el-option
          v-for="item in [
            {
              label: '出院',
              value: 'cysj_l',
            },
            {
              label: '入院',
              value: 'rysj_l',
            },
            {
              label: '结算',
              value: 'jsrq_l',
            },
          ]"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>

      <el-date-picker
        v-model="query.stime"
        value-format="yyyy-MM-dd"
        type="date"
        placeholder="开始日期"
        style="margin-left: 5px"
      >
      </el-date-picker>
      <span style="margin-left: 2px">至</span>
      <el-date-picker
        v-model="query.etime"
        value-format="yyyy-MM-dd"
        type="date"
        placeholder="结束日期"
        style="margin-left: 5px"
      >
      </el-date-picker>

      <el-select
        style="margin-left: 5px; width: 250px"
        v-model="query.ylfkfs"
        multiple
        collapse-tags
        filterable
        placeholder="医疗付款方式"
      >
        <el-option
          v-for="item in ykfs"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-input
        size="medium"
        placeholder="编码"
        suffix-icon="el-icon-search"
        style="width: 250px; margin-left: 5px"
        v-model="query.s"
        @keyup.enter.native="search"
      ></el-input>
      <el-button style="margin-left: 5px" type="primary" round @click="search"
        >查询</el-button
      >
    </el-row>

    <el-row>
      <el-table
        :data="ls2"
        style="width: 100%; margin-top: 5px"
        v-loading="loading"
        @sort-change="sortChange"
        border
      >
        <el-table-column
          prop="mdc"
          :sortable="true"
          label="编码"
          width="150"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="name" label="名称" align="center" width="150">
        </el-table-column>
        <el-table-column
          prop="docCount"
          :sortable="true"
          label="人次"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="drgcount"
          label="DRG数"
          :sortable="true"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="weight_sum"
          label="总点数"
          :formatter="fmt"
          align="center"
          :sortable="true"
        >
        </el-table-column>
        <el-table-column
          prop="weight_avg"
          label="CMI"
          align="center"
          :sortable="true"
        >
          <template slot-scope="scope">
            {{ (scope.row.weight_avg / 100).toFixed(4) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="benchmarkFee_sum"
          label="标准总费用"
          :formatter="fmt"
          align="center"
          :sortable="true"
          width="120"
        >
        </el-table-column>
        <el-table-column
          prop="zfy_sum"
          label="住院总费用"
          :formatter="fmt"
          align="center"
          :sortable="true"
          width="120"
        >
        </el-table-column>

        <el-table-column label="费用差" align="center">
          <template slot-scope="scope">
            {{ (scope.row.benchmarkFee_sum - scope.row.zfy_sum).toFixed(2) }}
          </template>
        </el-table-column>

        <el-table-column
          prop="benchmarkFee_avg"
          label="标准次均费用"
          :formatter="fmt"
          width="120"
          align="center"
        >
        </el-table-column>

        <el-table-column
          prop="zfy_avg"
          label="住院次均费用"
          :formatter="fmt"
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="sjzyts_l_avg"
          label="平均床日"
          :formatter="fmt"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="wk" label="外科组" align="center">
        </el-table-column>
        <el-table-column prop="cz" label="非手术室治疗组" align="center">
        </el-table-column>
        <el-table-column prop="nk" label="内科组" align="center">
        </el-table-column>
        <el-table-column prop="gdbl_1" label="高倍率" align="center">
        </el-table-column>
        <el-table-column prop="gdbl_-1" label="低倍率" align="center">
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="sizeChange"
          @current-change="curChange"
          :current-page="query.page"
          :page-sizes="[5, 10, 20, 50, 100, 200, 300, 400]"
          :page-size="query.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="query.totalElements"
        >
        </el-pagination>
      </div>
    </el-row>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { UserModule } from "@/store/modules/user";
import { pfmls } from "@/api/cloudwebapi";
import Pagination from "@/components/Pagination/index.vue";
import { cloneDeep } from "lodash";
import { Form } from "element-ui";
import { parseTime, dateFormat } from "@/utils";
import { dicList } from "@/api/datatranform";
@Component({
  name: "HospMdc",
  components: {
    Pagination,
  },
})
export default class extends Vue {
  private query = {
    s: "",
    stime: "",
    etime: dateFormat(new Date(), "yyyy-MM-dd"),
    time: "jsrq_l",
    grps: "mdc",
    page: 1,
    size: 10,
    totalElements: 0,
    wh: [{ f: "ingrp", op: "eq", vals: true }],
    ylfkfs: "",
  };
  private loading: boolean = true;

  private async sortChange(p: any) {
    let order = p.order;
    let prop = p.prop;
    let x = order == "ascending" ? 1 : -1;

    let ts: any = JSON.parse(JSON.stringify(this.ls));

    ts = ts.sort((a: any, b: any) => {
      if (isNaN(a[prop])) {
        return a[prop].localeCompare(b[prop]) * x;
      } else {
        return (a[prop] - b[prop]) * x;
      }
    });
    this.ls = JSON.parse(JSON.stringify(ts));
    ts = ts.slice(
      (this.query.page - 1) * this.query.size,
      this.query.page * this.query.size
    );
    this.ls2 = ts;
  }

  private search() {
    this.loadPfmls();
  }

  private sizeChange(val: any) {
    this.query.size = val;
  }

  private curChange(val: any) {
    this.query.page = val;
    this.ls2 = this.ls.slice(
      (this.query.page - 1) * this.query.size,
      this.query.page * this.query.size
    );
  }

  private fmt(row: any, c: any, val: any) {
    let p = c.property;

    if (isNaN(val)) return val;

    if (["weight_sum", "weight_avg"].includes(p)) {
      return val.toFixed(4);
    } else {
      try {
        return val.toFixed(2);
      } catch (e) {
        return val;
      }
    }
    //return val;
  }
  private mdc2name: any = {
    MDCA: "先期分组疾病及相关操作",
    MDCB: "神经系统疾病及功能障碍",
    MDCC: "眼疾病及功能障碍",
    MDCD: "头颈、耳、鼻、口、咽疾病及功能障碍",
    MDCE: "呼吸系统疾病及功能障碍",
    MDCF: "循环系统疾病及功能障碍",
    MDCG: "消化系统疾病及功能障碍",
    MDCH: "肝、胆、胰疾病及功能障碍",
    MDCI: "肌肉、骨骼疾病及功能障碍",
    MDCJ: "皮肤、皮下组织及乳腺疾病及功能障碍",
    MDCK: "内分泌、营养、代谢疾病及功能障碍",
    MDCL: "肾脏及泌尿系统疾病及功能障碍",
    MDCM: "男性生殖系统疾病及功能障碍",
    MDCN: "女性生殖系统疾病及功能障碍",
    MDCO: "妊娠、分娩及产褥期",
    MDCP: "新生儿及其他围产期新生儿疾病",
    MDCQ: "血液、造血器官及免疫疾病和功能障碍",
    MDCR: "骨髓增生疾病和功能障碍，低分化肿瘤",
    MDCS: "感染及寄生虫病（全身性或不明确部位的）",
    MDCT: "精神疾病及功能障碍",
    MDCU: "酒精/药物使用及其引起的器质性精神功能障碍",
    MDCV: "创伤、中毒及药物毒性反应",
    MDCW: "烧伤",
    MDCX: "影响健康因素及其他就医情况",
    MDCY: "HIV感染疾病及相关操作",
    MDCZ: "多发严重创伤",
  };
  private dicLs: Array<any> = [];
  private ykfs: Array<any> = [];
  private async loadDicLs() {
    const { data } = await dicList({ size: 1000 });
    return data.content;
  }
  private ls: Array<any> = [];
  private ls2: Array<any> = [];
  private async loadPfmls() {
    this.loading = true;
    let q: any = cloneDeep(this.query);
    q.stime = q.stime.replaceAll("-", "");
    q.etime = q.etime.replaceAll("-", "");
    if (q.ylfkfs && q.ylfkfs.length > 0) {
      q.ylfkfs = q.ylfkfs.join(",");
    }
    if (q.s) {
      q.s = q.s.toLowerCase();
      q.wh.push({ f: "mdc", op: "match", vals: q.s });
    }
    q.wh = JSON.stringify(q.wh);
    const { data } = await pfmls(q);
    if (data) {
      data.forEach((it: any) => {
        it.name = this.mdc2name[it.mdc];
      });
    }
    this.query.totalElements = data.length;
    this.ls = data;
    if (this.ls) {
      this.ls2 = this.ls.slice(0, this.query.size);
    }
    this.loading = false;
  }

  async created() {
    let cDate = new Date();
    // cDate.setMonth(cDate.getMonth() - 3);
    cDate.setMonth(cDate.getMonth()) // 修改默认开始时间为当月1号
    cDate.setDate(1)
    this.query.stime = dateFormat(cDate, "yyyy-MM-dd");
    this.dicLs = await this.loadDicLs();

    this.ykfs = this.dicLs
      .filter((it: any) => it.category == "医疗付款方式代码")
      .map((it: any) => ({ label: it.name, value: it.code }));
    this.loadPfmls();
  }
}
</script>

<style lang="scss">
.el-date-editor {
  width: 140px !important;
}
.error {
  color: red;
}
.pagination-container {
  float: right;
}
.el-dialog__body {
  padding: 10px !important;
}
</style>